﻿@inject IStringLocalizer<Foo> FooLocalizer
@inject IStringLocalizer<TablesColumnWidth> Localizer
@inject ToastService ToastService

<Table TItem="Foo" @ref="TableRows"
       IsPagination="true" PageItemsSource="@PageItemsSource"
       IsStriped="true" IsBordered="true"
       ShowToolbar="true" ShowDefaultButtons="false" ShowSearch="false" IsMultipleSelect="true"
       ShowExtendButtons="true" ExtendButtonColumnWidth="240"
       OnQueryAsync="@OnQueryAsync">
    <TableToolbarTemplate>
        <TableToolbarButton TItem="Foo" Icon="fa-fw fa-solid fa-pen-to-square" IsEnableWhenSelectedOneRow="true" Text="@Localizer["WidthButtonText1"]" OnClickCallback="@CustomerButton" Color="Color.Primary" />
        <TableToolbarButton TItem="Foo" Icon="fa-fw fa-solid fa-pen-to-square" IsEnableWhenSelectedOneRow="true" Text="@Localizer["WidthButtonText2"]" OnClickCallback="@CustomerButton" Color="Color.Success" />
        <TableToolbarButton TItem="Foo" Icon="fa-fw fa-solid fa-pen-to-square" IsEnableWhenSelectedOneRow="true" Text="@Localizer["WidthButtonText3"]" OnClickCallback="@CustomerButton" Color="Color.Warning" />
        <TableToolbarButton TItem="Foo" Icon="fa-fw fa-solid fa-pen-to-square" IsEnableWhenSelectedOneRow="true" Text="@Localizer["WidthButtonText4"]" OnClickCallback="@CustomerButton" Color="Color.Danger" />
    </TableToolbarTemplate>
    <TableColumns>
        <TableColumn @bind-Field="@context.DateTime" Width="180" />
        <TableColumn @bind-Field="@context.Name" Width="100" />
        <TableColumn @bind-Field="@context.Address" />
    </TableColumns>
    <RowButtonTemplate>
        <TableCellButton Color="Color.Primary" Icon="fa-solid fa-pen" Text="@Localizer["WidthButtonText1"]" OnClick="@(() => OnRowButtonClick(context, Localizer["WidthButtonText1"]))" />
        <TableCellButton Color="Color.Success" Icon="fa-solid fa-pen" Text="@Localizer["WidthButtonText2"]" OnClick="@(() => OnRowButtonClick(context, Localizer["WidthButtonText2"]))" />
        <TableCellButton Color="Color.Warning" Icon="fa-solid fa-pen" Text="@Localizer["WidthButtonText3"]" OnClick="@(() => OnRowButtonClick(context, Localizer["WidthButtonText3"]))" />
        <TableCellButton Color="Color.Danger" Icon="fa-solid fa-pen" Text="@Localizer["WidthButtonText4"]" OnClick="@(() => OnRowButtonClick(context, Localizer["WidthButtonText4"]))" />
        <TableCellPopconfirmButton Color="Color.Info" Icon="fa-solid fa-pen" Text="@Localizer["WidthConfirmButtonText"]" OnConfirm="@(() => OnRowButtonClick(context, Localizer["WidthConfirmButtonText"]))" />
    </RowButtonTemplate>
</Table>

@code {
    /// <summary>
    /// Foo 类为Demo测试用，如有需要请自行下载源码查阅
    /// Foo class is used for Demo test, please download the source code if necessary
    /// https://gitee.com/LongbowEnterprise/BootstrapBlazor/blob/main/src/BootstrapBlazor.Shared/Data/Foo.cs
    /// </summary>
    [NotNull]
    private List<Foo>? Items { get; set; }

    private static IEnumerable<int> PageItemsSource => new int[] { 5, 10, 20 };

    /// <summary>
    /// OnInitialized 方法
    /// </summary>
    protected override void OnInitialized()
    {
        base.OnInitialized();

        Items = Foo.GenerateFoo(FooLocalizer);
    }

    private Task<QueryData<Foo>> OnQueryAsync(QueryPageOptions options)
    {
        IEnumerable<Foo> items = Items;

        // 过滤
        var isFiltered = false;
        if (options.Filters.Any())
        {
            items = items.Where(options.Filters.GetFilterFunc<Foo>());
            isFiltered = true;
        }

        // 排序
        var isSorted = false;
        if (!string.IsNullOrEmpty(options.SortName))
        {
            var invoker = Foo.GetNameSortFunc();
            items = invoker(items, options.SortName, options.SortOrder);
            isSorted = true;
        }

        // 设置记录总数
        var total = items.Count();

        // 内存分页
        items = items.Skip((options.PageIndex - 1) * options.PageItems).Take(options.PageItems).ToList();

        return Task.FromResult(new QueryData<Foo>()
        {
            Items = items,
            TotalCount = total,
            IsSorted = isSorted,
            IsFiltered = isFiltered,
            IsSearch = true
        });
    }

    /// <summary>
    /// CustomerButton
    /// </summary>
    /// <param name="items"></param>
    private async Task CustomerButton(IEnumerable<Foo> items)
    {
        var cate = ToastCategory.Information;
        var title = Localizer["CustomerButtonTitle"];
        var content = Localizer["CustomerButtonContent", items.Count()];
        await ToastService.Show(new ToastOption()
        {
            Category = cate,
            Title = title,
            Content = content
        });
    }

    [NotNull]
    private Table<Foo>? TableRows { get; set; }

    private async Task OnRowButtonClick(Foo item, string text)
    {
        var cate = ToastCategory.Success;
        var title = $"{text} {item.Name}";
        var content = Localizer["OnRowButtonClickContent"];
        await ToastService.Show(new ToastOption()
        {
            Category = cate,
            Title = title,
            Content = content
        });

        await TableRows.QueryAsync();
    }
}
